package com.mayspeed.jg.command;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.mayspeed.jg.JingGong;
import com.mayspeed.jg.gui.JGGui;
import com.mayspeed.jg.util.JGUtil;

/**
 * 命令执行类
 * @author May_Speed
 * @since 2017/4/22
 */
public class JGCommand implements CommandExecutor {
	
	@SuppressWarnings("deprecation")
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		if(cmd.getName().equalsIgnoreCase("jg")) {
			
			if (args.length == 0) {
				sender.sendMessage("§6====== §7[§6武器精工§7] §6======");
				sender.sendMessage("§b/jg open §7打开精工面板");
				sender.sendMessage("§b/jg reload §7重载插件");
				return true;
			}
			
			if(!(sender instanceof Player)) {
				sender.sendMessage("§f你必须是一个玩家!");
				return true;
			}
			
			Player player = (Player) sender;
			if (args[0].equalsIgnoreCase("open")) {
				if (args.length != 1) {
					sender.sendMessage("§7[§6武器精工§7] §e> §c参数不正确!");
					return true;
				}
				if (sender.hasPermission("jg.open")) {
					/*玩家手中物品*/
					ItemStack is = JGUtil.getItemInMainHand(player);
					
					/*物品检查开始*/
					if (is.getType() == Material.AIR || is.getType() == null ) {
						sender.sendMessage(JingGong.getInstance().getMessage().getString("noItemInHand").replaceAll("&", "§"));
						return true;
					}
				    if (!(JingGong.getInstance().getConfig().getIntegerList("CanJgItem").contains(Integer.valueOf(is.getTypeId())))) {
				    	sender.sendMessage(JingGong.getInstance().getMessage().getString("noItemInList").replaceAll("&", "§"));
				    	return true;
				    }
				    /*检查结束*/
				    if (JGUtil.getItemJingGongLevel(JGUtil.getItemInMainHand(player)) == 5) {
				    	player.sendMessage(JingGong.getInstance().getMessage().getString("isMaxLevel").replaceAll("&", "§"));
						return true;
					}
					JGGui.openGui(player);
					return true;
				}
			}
			
			if (args[0].equalsIgnoreCase("reload")) {
				if (args.length != 1) {
					sender.sendMessage("§7[§6武器精工§7] §e> §c参数不正确!");
					return true;
				}
				if (sender.hasPermission("jg.reload")) {
					JingGong.getInstance().reloadConfig(); 
					player.sendMessage("§7[§6武器精工§7] §e§l> §a重载成功!");
				} else {
					sender.sendMessage("§7[§6武器精工§7] §e§l> §c权限不足!");
				}
				return true;
			}
		}
		
		return false;
	}
}
